package com.zjs.jvm;

import java.text.NumberFormat;

/**
 * @Description 获取gc内存信息
 * @Author zhu_jinsheng[zhu_js@suixingpay.com]
 * @Date 2020/1/8 13:14
 * @Version 1.0
 */
public class GCInfo {

    public static void getGcInfo(){
        //获取jvm全部内存大小
        double total = Runtime.getRuntime().totalMemory();
        //获取jvm空闲内存大小
        double free = Runtime.getRuntime().freeMemory();
        //百分比显示内存占比
        NumberFormat nf = NumberFormat.getPercentInstance();
        nf.setMinimumFractionDigits(1);
        String k = nf.format((total - free)/total);
        System.out.println(
                " 可用内存： " + total +
                " 剩余内存 ： " + + free +
                " 使用内存所占百分比： " + k);
    }

    public static void getGcInfo(int i){
        double total = Runtime.getRuntime().totalMemory();
        double free = Runtime.getRuntime().freeMemory();
        NumberFormat nf = NumberFormat.getPercentInstance();
        nf.setMinimumFractionDigits(1);
        String k = nf.format((total - free)/total);
        System.out.println(
                "第" + i + "次" +
                " 可用内存： " + total +
                " 剩余内存 ： " + + free +
                " 使用内存所占百分比： " + k);
    }
}
